<?php
/**
 * @Author: king
 * @Last Modified by:   king
 * Date: 2018/6/12
 * Time: 11:46
 */
namespace App\Models\Api;

use App\Models\Api\Base;
use Cache;

/**
 * 档案-档案袋 关系  中间表
 * Class RecodeCat
 * @package app\common\model
 */
/**
 * 档案-档案袋 关系  中间表
 * Class RecodeCat
 * @package app\common\model
 */
class RecodeCat extends Base
{
    protected $table = 'recode_cat';

    /**
     * 获取当前用户的档案袋
     */
    public function getRecodeCat($map = [])
    {
        $uid  = $map['uid'];
        $data = false;

        if (intval($this->cache_on) >= 1 && Cache::has('recode_cat_' . $uid)) {
            $data = Cache::get('recode_cat_' . $uid);
        }

        if ($data === false) {
            $data = $this->from('recode_cat as rc')
                ->select('rc.cid', 'rc.rid')
                ->join('recode as r', 'rc.rid', '=', 'r.id', 'INNER')
                ->where(['r.uid' => $uid])
                ->get();

            $data = json_decode(json_encode($data, true), true);

            if (intval($this->cache_on) >= 1) {
                Cache::set('recode_cat_' . $uid, $data, 86400 * 3);
            }
        }

        return $data;
    }

    public function getRecodeCatId($map = [])
    {
        $uid  = $map['uid'];
        $rid  = $map['rid'];
        $data = $this->getRecodeCat(['uid' => $uid]);

        $cid = null;

        foreach ($data as $v) {
            if ($v['rid'] == $rid) {
                $cid = $v['cid'];
            }
        }

        return $cid;
    }

}
